// utils/logger.js
import {
	ref
} from 'vue';

const logs = ref([]);

// 日志类型定义
const LogLevel = {
	LOG: 'log',
	WARN: 'warn',
	ERROR: 'error',
	INFO: 'info'
};

// 日志颜色映射
const LogColor = {
	[LogLevel.LOG]: '#35495e', // 深蓝色
	[LogLevel.INFO]: '#42b983', // 绿色
	[LogLevel.WARN]: '#e6a23c', // 橙色
	[LogLevel.ERROR]: '#f56c6c' // 红色
};

export const addLog = (message, level = LogLevel.LOG) => {
	logs.value.push({
		timestamp: new Date().toISOString(),
		message: typeof message === 'object' ? JSON.stringify(message, null, 2) : String(message),
		level,
		color: LogColor[level]
	});

	// 保持日志数量在合理范围内
	if (logs.value.length > 1000) {
		logs.value.shift();
	}
};

// 导出日志级别常量
export {
	LogLevel
};

export const getLogs = () => logs.value;
export const clearLogs = () => {
	logs.value = [];
};

// 重写console方法
export const initConsoleOverride = () => {
	const originalConsole = {
		...console
	};

	console.log = function() {
		addLog(Array.from(arguments).join(' '), LogLevel.LOG);
		originalConsole.log.apply(originalConsole, arguments);
	};

	console.info = function() {
		addLog(Array.from(arguments).join(' '), LogLevel.INFO);
		originalConsole.info.apply(originalConsole, arguments);
	};

	console.warn = function() {
		addLog(Array.from(arguments).join(' '), LogLevel.WARN);
		originalConsole.warn.apply(originalConsole, arguments);
	};

	console.error = function() {
		addLog(Array.from(arguments).join(' '), LogLevel.ERROR);
		originalConsole.error.apply(originalConsole, arguments);
	};
};